package cn.anzhiyublog.staranimegallery.exception;

import cn.anzhiyublog.staranimegallery.model.enums.StatusCode;
import cn.anzhiyublog.staranimegallery.model.response.Response;
import cn.anzhiyublog.staranimegallery.util.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常捕捉
 *
 * @author 安知一只鱼
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(GalleryException.class)
    public Response<?> customExceptionHandler(GalleryException e){
        log.error("GalleryException",e);
        return ResultUtils.error(e.getCode(),e.getMessage());
    }

    /**
     * 捕获预期外的异常
     */
    @ExceptionHandler(RuntimeException.class)
    public Response<?> runtimeExceptionHandler(RuntimeException e){
        log.error("RuntimeException",e);
        return ResultUtils.error(StatusCode.INTERNAL_SERVER_ERROR,"系统内部异常");
    }
}
